package com.priv.service.impl;

import com.priv.controller.Code;
import com.priv.dao.BookDao;
import com.priv.domain.Book;
import com.priv.exception.BusinessException;
import com.priv.exception.SystemException;
import com.priv.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 十一
 * @data : 13:05 2022/12/28
 * When in doubt, use brute force.
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    /**
     * 添加
     *
     * @param book
     * @return
     */
    public boolean save(Book book) {
        return bookDao.save(book) > 0;
    }

    /**
     * 更新
     *
     * @param book
     * @return
     */
    public boolean update(Book book) {
        return bookDao.update(book) > 0;
    }

    /**
     * 按id删除
     *
     * @param id
     * @return
     */
    public boolean delete(Integer id) {
        return bookDao.delete(id) > 0;
    }

    /**
     * 按id查询
     *
     * @param id
     * @return
     */
    public Book findById(Integer id) {
//        if (id == 1){
//            throw new BusinessException(Code.BUSINESS_ERR,"出异常了！");
//        }
//        try {
//            int i = 1/0;
//        }catch (Exception e){
//            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时，请重试！",e);
//        }
        return bookDao.findById(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    public List<Book> findAll() {
        return bookDao.findAll();
    }
}
